import { Metadata } from "next";
import { notFound } from "next/navigation";
import { createServerClient } from "~/trpc/server-client";
import { CourseForm } from "~/components/courses/CourseForm";
import { ErrorBoundary } from "~/components/common/ErrorBoundary";
import type { CoursePageParams } from "~/types/route";

interface Props {
  params: CoursePageParams;
}

export const metadata: Metadata = {
  title: "编辑课程 - 在线教育平台",
  description: "编辑课程信息",
};

export default async function CourseEditPage({ params }: Props) {
  // 确保 params.courseId 是有效的
  if (!params.courseId) {
    notFound();
  }

  const trpc = await createServerClient();
  const courseId = params.courseId;
  
  try {
    const course = await trpc.courses.getById.fetch({ 
      courseId 
    });

    if (!course) {
      notFound();
    }

    return (
      <div className="p-6">
        <h1 className="text-2xl font-bold mb-6">编辑课程</h1>
        <ErrorBoundary>
          <CourseForm 
            mode="page"
            initialValues={course}
          />
        </ErrorBoundary>
      </div>
    );
  } catch (error) {
    notFound();
  }
} 